<template>
  <div>
    <el-row :gutter="20">
      <el-col :span="12">
        <el-card shadow="hover">
          <template #header>
            <div class="clearfix">
              <span>个人信息</span>
            </div>
          </template>
          <div class="info">
            <div class="info-name">用户名： <span>{{ userStore.personalInfo.realname }}</span></div>
            <div class="info-name">电话: <span>{{ userStore.personalInfo.phoneNo }}</span></div>
            <div class="info-name">所属角色: <span>{{ userStore.personalInfo.roleName }}</span></div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="12">
        <el-card shadow="hover">
          <template #header>
            <div class="clearfix">
              <span>账户编辑</span>
            </div>
          </template>
          <el-form :model="userInfoForm" ref="passwordRef" label-width="90px">
            <el-form-item label="旧密码：">
              <el-input type="password" v-model="userInfoForm.oldPassword"></el-input>
            </el-form-item>
            <el-form-item label="新密码：">
              <el-input type="password" v-model="userInfoForm.newPassword"></el-input>
            </el-form-item>
            <el-form-item label="确认密码：">
              <el-input type="password" v-model="userInfoForm.confirmPassword"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="onSubmit(passwordRef)">保存</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref, onMounted } from 'vue'
import { useUserStore } from "@/store/modules/user"
import { changePassword } from '@/api/user'
import { useRouter } from 'vue-router'
import type { UserInfoForm } from './type'
let userStore: any = useUserStore()
const $router = useRouter()

const userInfoForm = reactive<UserInfoForm>({
  oldPassword: '',
  newPassword: '',
  confirmPassword: '',
  userId: userStore.personalInfo.id
})

const passwordRef = ref()

const onSubmit = async (formEl: any) => {
  if (!formEl) return
  await formEl.validate(async (valid: any, fields: any) => {
    if (valid) {
      await changePassword(userInfoForm)
      $router.push('/login')
    } else {
      console.log('error submit!', fields)
    }
  })
}

</script>

<style lang="scss" scoped>
.info {
  text-align: left;

  .info-name {
    margin: 15px 0 10px;
    font-size: 18px;
    font-weight: 500;

    span {
      font-size: 20px;
      font-weight: 500;
    }
  }
}
</style>
